package longrise.phone.com.bjjt_henan.myaccident;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.longrise.android.FormParameter;
import com.longrise.android.LFView;

import longrise.phone.com.bjjt_henan.R;

/**
 * 页面选择
 */
public abstract class PageView extends LFView implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener {

    public static final String TAG = "PageView";

    private Context mContext;

    private View mView;

    private RadioGroup mRGroup;

    private RadioButton mLeftRBtn;

    private RadioButton mRightRBtn;

    private View mLeftView;

    private View mRightView;

    private ViewPager mViewPager;

    public PageView(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        if (null == mContext) {
            return;
        }
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_page, null);
        if (null == mView) {
            return;
        }
        mRGroup = (RadioGroup) mView.findViewById(R.id.page_rg);
        mLeftRBtn = (RadioButton) mView.findViewById(R.id.page_rb_left);
        mRightRBtn = (RadioButton) mView.findViewById(R.id.page_rb_right);
        mLeftView = (View) mView.findViewById(R.id.page_view_left);
        mRightView = (View) mView.findViewById(R.id.page_view_right);
        mViewPager = (ViewPager) mView.findViewById(R.id.page_vp);

        setData();
        setListener(true);
    }

    public void setData() {
        try {
            String[] titles = getTitles();
            if (null == titles || titles.length < 2) {
                throw new Exception("pageView的getTitles()获取数目不对");
            }
            mLeftRBtn.setText(titles[0]);
            mRightRBtn.setText(titles[1]);

            PagerAdapter adapter = getAdapter();
            if (null == adapter) {
                throw new Exception("pageView的getAdapter()设置有误");
            }
            mViewPager.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取标题（数组长度至少为2）
    public abstract String[] getTitles();

    //获取适配器
    public abstract PagerAdapter getAdapter();

    //改变选中项的监听
    public abstract void itemChange(int position);

    public void setListener(boolean flag) {
        if (null != mRGroup) {
            mRGroup.setOnCheckedChangeListener(flag ? this : null);
        }
        if (null != mViewPager) {
            mViewPager.setOnPageChangeListener(flag ? this : null);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int count = this.getAdapter().getCount();
        switch (checkedId) {
            case R.id.page_rb_left:
                if (count >= 1) {
                    mViewPager.setCurrentItem(0, true);
                } else {
                    mLeftView.setVisibility(View.VISIBLE);
                    mRightView.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.page_rb_right:
                if (count >= 2) {
                    mViewPager.setCurrentItem(1, true);
                } else {
                    mLeftView.setVisibility(View.INVISIBLE);
                    mRightView.setVisibility(View.VISIBLE);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            mLeftRBtn.setChecked(true);
            mLeftView.setVisibility(View.VISIBLE);
            mRightView.setVisibility(View.INVISIBLE);
        } else if (position == 1) {
            mRightRBtn.setChecked(true);
            mLeftView.setVisibility(View.INVISIBLE);
            mRightView.setVisibility(View.VISIBLE);
        }
        itemChange(position);
    }

    @Override
    public FormParameter getFormParameter() {
        return null;
    }

    @Override
    public void refresh() {

    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        setListener(false);
        mLeftView = null;
        mLeftView = null;
        mLeftRBtn = null;
        mLeftRBtn = null;
        if (null != mViewPager) {
            mViewPager.setAdapter(null);
        }
        mViewPager = null;
        mView = null;
    }
}
